(function() {

    function SuperClass() {
        this.superValue = "super value";
        this.values = ['one', 'two', 'three'];
    }

    SuperClass.prototype.getSuperValue = function() {
        return this.value;
    };

    function SubClass() {
        this.subValue = "sub value";
    }

    SubClass.prototype = new SuperClass();

    SubClass.prototype.getSubValue = function() {
        return this.subValue;
    };


    let obj = new SubClass();
    console.log(obj.getSubValue());
    console.log(obj.getSubValue());
    obj.values.push('new value');

    let obj2 = new SubClass();
    console.log(obj.values);


    //存在的问题
    //1. 父类中的引用属性会被子类共享
    //2. 父类只能使用默认构造函数
}());


(function() {

    function SuperClass(id) {
        this.id = id;
        this.books = ['javascript', 'html', 'css'];
    }

    SuperClass.prototype.showBooks = function() {
        console.log(this.books);
    };

    function SubClass(id) {
        //继承父类
        SuperClass.call(this, id);
    }

    let obj1 = new SubClass(1);
    obj1.books.push('new book');
    obj1.showBooks();
    let obj2 = new SubClass(2);
    obj2.showBooks();

    //问题：父类的proto方法不会被子类继承

}());


(function() {

    function SuperClass(name) {
        this.name = name;
        this.books = ["html", "css", "javascript"];
    }

    SuperClass.prototype.getName = function() {
        return this.name;
    };

    function SubClass(name, time) {
        SuperClass.call(this, name);
        this.time = time;
    }
    //use super class prototype
    SubClass.prototype = new SuperClass();
    //define sub class method
    SubClass.prototype.getTime = function() {
        return this.time;
    };


    let obj1 = new SubClass('js book', 2014);
    obj1.books.push('design');
    console.log(obj1.books);

    let obj2 = new SubClass('css', 2013);
    console.log(obj2.books);

    //问题：在设置子类的原型类时多调用了一次SuperClass的构造
}());

(function(){

    function inheritObject(o) {
        function F() {}
        F.prototype = o;
        return new F();
    }

    let book = {
        name: 'js book',
        alikeBook: ['css book', 'html book']
    };

    function createBook(obj) {
        let o = inheritObject(book);
        o.getName = function() {
            console.log(name);
        };
        return o;
    }

}());


(function(){

    function inheritObject(o) {
        function F() {}
        F.prototype = o;
        return new F();
    }


    function inheritPrototype(subClass, superClass) {
        //包装并直接使用父类的原型，避免了对父类对象的构造
        let p = inheritObject(superClass.prototype);
        //修正constructor指向
        p.constructor = subClass;
        subClass.prototype = p;
    }

    function SuperClass(name) {
        this.name = name;
    }

    SuperClass.prototype.getName = function() {
        return this.name;
    };

    function SubClass(name, time) {
        SuperClass.call(this, name);
        this.time = time;
    }

    //设置SubClass原型
    inheritPrototype(SubClass, SuperClass);

    //增加SubClass方法
    SubClass.prototype.getTime = function() {
        return this.getTime();
    };

    let obj1 = new SubClass("js book", 2014);
    let obj2 = new SubClass("css book", 2013);

}());